public class Test11 {
    public static void main(String[] args) {
      Test11 test11 = new Test11();
      String s = "MIIIV";
        System.out.println("sum: "+test11.romanToInt(s));
    }
    public int romanToInt(String s) {
        int sum = getValue(s.charAt(s.length()-1)); ;
        int num = 0;
        int preNum = getValue(s.charAt(0));
        System.out.println("preNum: "+preNum);
        for(int i = s.length()-2,j=s.length()-1;i >= 0; i--) {
            num = getValue(s.charAt(j));
            preNum = getValue(s.charAt(i));
          if(num > preNum) {
              sum -= preNum;
          }else {
              sum += preNum;
              j = i;
          }
        }
        return sum;
    }
    public int getValue(char ch) {
        switch (ch) {
            case 'I':
                return 1;
            case 'V':
                return 5;
            case 'X':
                return 10;
            case 'L':
                return 50;
            case 'C':
                return 100;
            case 'D':
                return 500;
            case 'M':
                return 1000;
            default:
                return 0;
        }

    }
}
